﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CharacterTrailClient.Windows.Exceptions;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using CharacterTrailLibrary.StaticData;
using DevExpress.XtraEditors;
using Telegnosis.Framework.Constants;

namespace CharacterTrailClient.Windows
{
    public partial class FormBomEdit : DevExpress.XtraEditors.XtraForm
    {

        private TBomHeaderModel _bomHeaderModel;
        private readonly IBomHeaderPersist _bomHeaderPersist;
        private readonly bool _isEditing = false;
        public FormBomEdit()
        {
            InitializeComponent();
            _bomHeaderPersist = InstanceFactory.GetBomHeaderPersis();
            if (GlobalClass.RunTimeMode)
            {
                Binding.DataBinder.BindingProductItem(this.gridLookUpEditParentItemNumber, this.textEditParentItemId, this.textEditParentItemName);
                Binding.DataBinder.BindingProductItem(this.gridLookUpEditChildItemNumber, this.textEditChildItemId, this.textEditChildItemName);
                Binding.DataBinder.BindingProcess(this.lookUpEditProcessNo, this.textEditProcessName);
                NewBindingModel();
            }
        }

        private void NewBindingModel()
        {
            _bomHeaderModel = new TBomHeaderModel();
            _bomHeaderModel.EditFlag = Enums.EditFlag.New;
            _bomHeaderModel.Qty = 1;
            _bomHeaderModel.RowGuid = Guid.NewGuid().ToString("N");
            foreach (var control in groupControl1.Controls)
            {
                if (control is BaseEdit)
                {
                    var edit = (BaseEdit)control;
                    edit.DataBindings.Clear();
                    edit.DataBindings.Add("EditValue", _bomHeaderModel, edit.Tag.ToString());
                }
            }
        }

        private bool ValidateWhetherExistBom(TBomHeaderModel bomHeaderModel,out string msg)
        {
            msg = "";
            string condition = " ParentItemNumber='{0}' and ChildItemNumber='{1}' ";
            condition = string.Format(condition, bomHeaderModel.ParentItemNumber, bomHeaderModel.ChildItemNumber);
            var list = _bomHeaderPersist.GetTBomHeaderModels(condition);
            if (list.Count > 0)
            {
                msg = "重复的产品/物料";
                return false;
            }
            return true;
        }

        private bool Save()
        {
            try
            {
                if (this.gridLookUpEditParentItemNumber.EditValue == null ||
                    String.IsNullOrEmpty(gridLookUpEditParentItemNumber.EditValue.ToString().Trim()))
                {
                    Message.MsgBox.ShowWarning("必须选择产品代码！");
                    return false;
                }

                if (String.IsNullOrEmpty(this.lookUpEditProcessNo.Text.Trim()))
                {
                    Message.MsgBox.ShowWarning("必须选择工序代码！");
                    return false;
                }

                if (String.IsNullOrEmpty(this.gridLookUpEditChildItemNumber.Text.Trim()))
                {
                    Message.MsgBox.ShowWarning("必须选择物料代码！");
                    return false;
                }

                Decimal qty = 0;
                if (String.IsNullOrEmpty(this.calcEditQty.Text.Trim()) || ! decimal.TryParse(this.calcEditQty.Text.Trim(),out qty))
                {
                    Message.MsgBox.ShowWarning("必须输入物料数量！");
                    return false;
                }

                if(qty<1)
                {
                    Message.MsgBox.ShowWarning("物料数量不能小于1！");
                    return false;
                }

                string msg;
                if (ValidateWhetherExistBom(_bomHeaderModel, out msg))
                {
                    _bomHeaderModel.BomNumber = _bomHeaderModel.ParentItemNumber;
                    _bomHeaderPersist.SaveBomHeader(this._bomHeaderModel);
                    return true;
                }
                else
                {
                    Message.MsgBox.ShowWarning(msg);
                    return false;
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.ShowException(ex);
                return false;
            }
        }

        private void CmdSave3_Click(object sender, EventArgs e)
        {
            try
            {
                if (Save())
                {
                    Message.MsgBox.ShowInfomation("保存成功！");
                    DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.ShowException(ex);
            }
        
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void CmdSaveNew_Click(object sender, EventArgs e)
        {
            if (Save())
            {
                NewBindingModel();
            }
        }
    }
}